import { defineStore, storeToRefs } from 'pinia';
import { User } from '@/store/user';
const userInfo = User();
const { roleInfo } = storeToRefs(userInfo);
import { Page } from '@/store/page';
const page = Page();
const { settings } = storeToRefs(page);


interface ChatState {
  usesrType: any, // 角色
  requestid: any, //请求id
  todeviceid: any, //对方设备ID
  chatitemList: [], // 全部好友列表
  moneyauth: number, // 转账权限
  muteuser: number, // 禁言权限
  mutetype: any, // 禁言权限
  textauth: number, // 文字发送权限
  picauth: number, // 图片发送权限
  vioauth: number, // 视频发送权限
  momoauth: number, // 表情发送权限
  autoreply: boolean, // 自动回复
  autoreplytext: any, // 自动回复
}

export const Chat = defineStore('chat', {

  state: (): ChatState => ({
    usesrType: 0, // 0 普通用户；1客服；2代理；3推广
    requestid: 5000, //请求id  
    todeviceid: 2654917, //对方设备ID
    chatitemList: [], //对方设备ID
    moneyauth: 0, // 转账权限
    muteuser: 0, // 禁言权限
    mutetype: {}, // 禁言权限
    textauth: 0, // 禁言权限
    picauth: 0, // 禁言权限
    vioauth: 0, // 禁言权限
    momoauth: 0, // 禁言权限
    autoreply: false, // 自动回复
    autoreplytext: '', // 自动回复
  }),
  actions: {
    async setRequestid(value: number) {
      this.requestid = value
    },
    async setTodeviceid(value: number) {
      this.todeviceid = value
    },
    async setChatitemList(value: any) {
      console.log('setChatitemList', value)
      this.chatitemList = value
    },
    async setUsesrType(value: any) {
      this.usesrType = value
    },
    async setMoneyauth(value: any) {
      this.moneyauth = value
    },
    async setMuteuser(value: any) {
      this.muteuser = value
    },
    async setAutoreply(value: boolean) {
      this.autoreply = value
    },
    async setAutoreplyText(value: any) {
      this.autoreplytext = value
    },
    async getConfig() {
      if (settings.value.customer_server) {
        let device_id = roleInfo.value.id || localStorage.getItem('deviceid')
        console.log('settings.value.customer_server', settings.value.customer_server + `/api/user/info?device_id=${device_id}`)
        // 获取角色
        // fetch(settings.value.customer_server + `/api/user/info?device_id=2650684`)
        fetch(settings.value.customer_server + `/api/user/info?device_id=${device_id}`)
          .then(response => {
            if (!response.ok) {
              throw new Error('Network response was not ok ' + response.statusText);
            }
            return response?.json(); // 将响应解析为 JSON
          })
          .then(res => {
            let data = res?.data
            console.log('角色', res)
            this.usesrType = data?.user_type || 0
            this.moneyauth = data?.moneyauth || 0
            this.muteuser = data?.muteuser || 0
            this.mutetype = data?.mutetype || 0
            // this.textauth = data?.textauth || 0
            // this.picauth = data?.picauth || 0
            // this.vioauth = data?.vioauth || 0
            // this.momoauth = data?.momoauth || 0
          })
      }
    }
  }
});
